package com.spider.controller;


import com.aliyun.oss.OSS;
import com.aliyuncs.exceptions.ClientException;
import com.spider.mapper.OssMapper;
import com.spider.oss.FileInfo;
import com.spider.oss.OssJavaSdkQuickStart;
import com.spider.oss.OssServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/OssController")
public class OssController {
    @Autowired
    private OSS ossClient;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    @Autowired
    OssMapper ossMapper;
    @Autowired
    OssJavaSdkQuickStart ossJavaSdkQuickStart;
    @Autowired
    OssServiceImpl ossService;

    @GetMapping("synListSoftLink")
    public String synListSoftLink() {
        // 1. 获取原目录文件列表
        List<FileInfo> originFiles = ossJavaSdkQuickStart.listFilesAndCreateSymlinks("COA_DOC", ossClient, bucketName,"COA_DOC_SIMPLE");

        // 2. 在 COA_AUTO2 下创建软链接副本
        List<FileInfo> auto2Files = ossJavaSdkQuickStart.createAuto2Symlinks(originFiles, ossClient, bucketName);

        // 3. 批量插入数据库
        int batchSize = 300;
        for (int i = 0; i < auto2Files.size(); i += batchSize) {
            int end = Math.min(i + batchSize, auto2Files.size());
            ossMapper.saveFiles(auto2Files.subList(i, end));
        }
        return "success";
    }

    @GetMapping("synList")
    public String synList(){
        List<FileInfo> fileInfos = ossJavaSdkQuickStart.listFilesAndCreateSymlinks("COA_DOC", ossClient, bucketName,"COA_DOC_SIMPLE");
        // 每 2000 条数据进行一次批量插入
        int batchSize = 300;
        int num=1;
        for (int i = 0; i < fileInfos.size(); i += batchSize) {
            log.info("第{}次批量插入",num);
            int endIndex = Math.min(i + batchSize, fileInfos.size());
            List<FileInfo> batchFiles = fileInfos.subList(i, endIndex);
            ossMapper.saveFiles(batchFiles);
        }
        return "success";
    }

/*    @GetMapping("synSqlserverList")
    public String synSqlserverList(){
        List<FileInfo> fileInfos = ossJavaSdkQuickStart.synSqlserverList();
        for(FileInfo fileInfo:fileInfos){
            ossService.jdbcBatchInsertV5(fileInfo);
        }
        return "success";
    }*/

    @GetMapping("uploadFileAll")
    public String uploadFileAll(@RequestParam(value = "secondFolder",defaultValue = "1")String secondFolder,
                                @RequestParam(value = "num",defaultValue = "10")int num) throws ClientException {
        // 修改为传入本地目录路径
        List<FileInfo> fileInfos = ossJavaSdkQuickStart.uploadLocalFile(
                "COA_PDF",
                "D:\\COA_PDF\\pdf20250325\\"+secondFolder,
                ossClient,
                bucketName,
                num
        );
        return "success 上传文件数量：" + fileInfos.size();
    }

    @GetMapping("uploadFile")
    public String uploadFile(@RequestParam(value = "num",defaultValue = "10")int num) throws ClientException {
        // 修改为传入本地目录路径
        List<FileInfo> fileInfos = ossJavaSdkQuickStart.uploadLocalFile(
                "COA_PDF",
                "D:\\COA_PDF",
                ossClient,
                bucketName,
                num
        );
        return "success 上传文件数量：" + fileInfos.size();
    }

    @GetMapping("test")
    public String test(){
        List<FileInfo> fileInfos = OssJavaSdkQuickStart.listFilesInFolder("COA_DOC", ossClient, bucketName);
        List<FileInfo> testList =new ArrayList<>();
        FileInfo firstFileInfo = fileInfos.get(0);
        testList.add(firstFileInfo);
        log.info("testList长度为"+testList.size()+testList.get(0).getFilename()+"/"+testList.get(0).getUrl());
        ossMapper.saveFiles(testList);
        return "success";
    }
}
